package com.mangofactory.aop.configuration
{
	import com.mangofactory.aop.interceptors.AspectInvokingInterceptor;
	
	import flash.utils.getDefinitionByName;
	
	import org.as3commons.bytecode.abc.ClassInfo;
	import org.as3commons.bytecode.proxy.ClassProxyInfo;
	import org.as3commons.bytecode.proxy.ProxyFactory;
	import org.as3commons.reflect.Type;

	public class ClassProxyDefinition
	{
		private var _type:Type;
		private var methodProxyHash:Object = {};
		private var methodProxies:Vector.<MethodProxyDefinition> = new Vector.<MethodProxyDefinition>();
		
		public function ClassProxyDefinition(type:Type)
		{
			_type = type;
		}
		
		public function get requiresProxy():Boolean
		{
			return methodProxies.length > 0;
		}

		public function getClass():Class
		{
			return _type.clazz;
		}
		public function registerMethodProxy(methodProxy:MethodProxyDefinition):void
		{
			unregisterExistingProxy(methodProxy.methodName);
			methodProxyHash[methodProxy.methodName] = methodProxy;
			methodProxies.push(methodProxy);
		}

		private function unregisterExistingProxy(methodName:String):void
		{
			var existingProxy:MethodProxyDefinition = methodProxyHash[methodName];
			if (existingProxy)
			{
				methodProxies.splice(methodProxies.indexOf(existingProxy),1);
			}
			delete methodProxyHash[methodName];
		}
		public function get methodProxyDefinitions():Vector.<MethodProxyDefinition>
		{
			return methodProxies;
		}

		public function getInterceptor(methodName:String):AspectInvokingInterceptor
		{
			var definition:MethodProxyDefinition = MethodProxyDefinition(methodProxyHash[methodName]);
			if (!definition)
				return null;
			return definition.interceptor;
		}
	}
}